// pages/card-join/index.js
const { PageBase } = require('../../mixins/page');
const { getMemberCard } = require('../../api/member');
const { getUserInfo } = require('../../biztools/info');
const { navBack, tipError, tipSuccess } = require('../../common/platform');

Page({
    mixins: [PageBase],
    data: {
        nickname: '',
        mobile: '',
        gender: 1,
        birthday: '',
        avatar: '',
        showModal: false,
        disabledMobile: false,
    },
    onLoad: function (options) {
        const memberInfo = getUserInfo();
        this.setData({
            avatar: memberInfo.avatar,
            nickname: memberInfo.nickname,
            mobile: memberInfo.mobile,
            gender: memberInfo.gender,
            birthday: memberInfo.birthday,
            disabledMobile: memberInfo.mobile ? true : false,
        });
    },
    // 选择
    checkGender(event) {
        const { gender } = event.target.dataset;
        this.setData({
            gender,
        });
    },
    // 选择生日
    bindDateChange: function(e) {
        this.setData({
            birthday: e.detail.value,
        });
    },
    // 名字改变
    nickNameChange(event) {
        const { value } = event.detail;
        this.setData({
            nickname: value,
        });
    },
    // 手机变化
    mobileChange(event) {
        const { value } = event.detail;
        this.setData({
            mobile: value,
        });
    },
    // 加入
    async joinAction() {
        const { nickname, mobile, gender, birthday } = this.data;
        const params = {
            nickname,
            mobile,
            gender,
            birthday,
        };
        const flag = await getMemberCard(params);
        if (flag) {
            // tipSuccess('领取成功');
            this.setData({
                showModal: true,
            });
            // navBack();
        } else {
            tipError('领取失败');
        }
    },
    handleModalClose() {
        navBack();
    },
})